﻿//
// Copyright 2024 Exhunter Technology Co.Ltd2.
//

using System;
using System.Configuration;
using X18.Framework.Core.Utils;

namespace X18.Framework.Core.Config
{
    /// <summary>
    /// 配置管理器
    /// </summary>
    public class Configur
    {
        /// <summary>
        /// 获取整数型值
        /// </summary>
        public static int GetInteger(string key, int defaultValue = 0)
        {
            int rst = defaultValue;
            var tmp = ConfigurationManager.AppSettings[key];
            if (tmp.IsNullOrEmptyOrWhiteSpace())
            {
                return rst;
            }
            try
            {
                rst = Convert.ToInt32(tmp);
            }
            catch
            {
                // Ignore
            }
            return rst;
        }

        /// <summary>
        /// 获取长整数型值
        /// </summary>
        public static long GetLong(string key, long defaultValue = 0L)
        {
            long rst = defaultValue;
            var tmp = ConfigurationManager.AppSettings[key];
            if (tmp.IsNullOrEmptyOrWhiteSpace())
            {
                return rst;
            }
            try
            {
                rst = Convert.ToInt64(tmp);
            }
            catch
            {
                // Ignore
            }
            return rst;
        }

        /// <summary>
        /// 获取日期型值
        /// </summary>
        public static DateTime GetDateTime(string key)
        {
            return GetDateTime(key, DateTime.MinValue);
        }

        /// <summary>
        /// 获取日期型值
        /// </summary>
        public static DateTime GetDateTime(string key, DateTime defaultValue)
        {
            DateTime rst = defaultValue;
            var tmp = ConfigurationManager.AppSettings[key];
            if (tmp.IsNullOrEmptyOrWhiteSpace())
            {
                return rst;
            }
            try
            {
                rst = Convert.ToDateTime(tmp);
            }
            catch
            {
                // Ignore
            }
            return rst;
        }

        /// <summary>
        /// 获取小数型值
        /// </summary>
        public static decimal GetDecimal(string key, decimal defaultValue = 0m)
        {
            decimal rst = defaultValue;
            var tmp = ConfigurationManager.AppSettings[key];
            if (tmp.IsNullOrEmptyOrWhiteSpace())
            {
                return rst;
            }
            try
            {
                rst = Convert.ToDecimal(tmp);
            }
            catch
            {
                // Ignore
            }
            return rst;
        }

        /// <summary>
        /// 获取字符串值
        /// </summary>
        public static string GetString(string key, string defaultValue = "")
        {
            var rst = ConfigurationManager.AppSettings[key];
            if (rst.IsNullOrEmptyOrWhiteSpace())
            {
                rst = defaultValue;
            }
            return rst;
        }

        /// <summary>
        /// 获取对象类型
        /// </summary>
        public static T Get<T>(string key)
        {
            T rst = default(T);
            try
            {
                rst = (T)ConfigurationManager.GetSection(key);
            }
            catch
            {
                // Ignore
            }
            return rst;
        }
    }
}
